ortools.sat.cp_model_pb2
Generated protocol buffer code.
1# -*- coding: utf-8 -*- 2# Generated by the protocol buffer compiler. DO NOT EDIT! 3# source: ortools/sat/cp_model.proto 4"""Generated protocol buffer code.""" 5from google.protobuf import descriptor as _descriptor 6from google.protobuf import descriptor_pool as _descriptor_pool 7from google.protobuf import symbol_database as _symbol_database 8from google.protobuf.internal import builder as _builder 9# @@protoc_insertion_point(imports) 10 11_sym_db = _symbol_database.Default() 12 13 14 15 16DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1aortools/sat/cp_model.proto\x12\x17operations_research.sat\"4\n\x14IntegerVariableProto\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x0e\n\x06\x64omain\x18\x02 \x03(\x03\"%\n\x11\x42oolArgumentProto\x12\x10\n\x08literals\x18\x01 \x03(\x05\"E\n\x15LinearExpressionProto\x12\x0c\n\x04vars\x18\x01 \x03(\x05\x12\x0e\n\x06\x63oeffs\x18\x02 \x03(\x03\x12\x0e\n\x06offset\x18\x03 \x01(\x03\"\x94\x01\n\x13LinearArgumentProto\x12>\n\x06target\x18\x01 \x01(\x0b\x32..operations_research.sat.LinearExpressionProto\x12=\n\x05\x65xprs\x18\x02 \x03(\x0b\x32..operations_research.sat.LinearExpressionProto\"\\\n\x1b\x41llDifferentConstraintProto\x12=\n\x05\x65xprs\x18\x01 \x03(\x0b\x32..operations_research.sat.LinearExpressionProto\"E\n\x15LinearConstraintProto\x12\x0c\n\x04vars\x18\x01 \x03(\x05\x12\x0e\n\x06\x63oeffs\x18\x02 \x03(\x03\x12\x0e\n\x06\x64omain\x18\x03 \x03(\x03\"E\n\x16\x45lementConstraintProto\x12\r\n\x05index\x18\x01 \x01(\x05\x12\x0e\n\x06target\x18\x02 \x01(\x05\x12\x0c\n\x04vars\x18\x03 \x03(\x05\"\xd3\x01\n\x17IntervalConstraintProto\x12=\n\x05start\x18\x04 \x01(\x0b\x32..operations_research.sat.LinearExpressionProto\x12;\n\x03\x65nd\x18\x05 \x01(\x0b\x32..operations_research.sat.LinearExpressionProto\x12<\n\x04size\x18\x06 \x01(\x0b\x32..operations_research.sat.LinearExpressionProto\"-\n\x18NoOverlapConstraintProto\x12\x11\n\tintervals\x18\x01 \x03(\x05\"F\n\x1aNoOverlap2DConstraintProto\x12\x13\n\x0bx_intervals\x18\x01 \x03(\x05\x12\x13\n\x0by_intervals\x18\x02 \x03(\x05\"\xb1\x01\n\x19\x43umulativeConstraintProto\x12@\n\x08\x63\x61pacity\x18\x01 \x01(\x0b\x32..operations_research.sat.LinearExpressionProto\x12\x11\n\tintervals\x18\x02 \x03(\x05\x12?\n\x07\x64\x65mands\x18\x03 \x03(\x0b\x32..operations_research.sat.LinearExpressionProto\"\xea\x01\n\x18ReservoirConstraintProto\x12\x11\n\tmin_level\x18\x01 \x01(\x03\x12\x11\n\tmax_level\x18\x02 \x01(\x03\x12\x42\n\ntime_exprs\x18\x03 \x03(\x0b\x32..operations_research.sat.LinearExpressionProto\x12\x45\n\rlevel_changes\x18\x06 \x03(\x0b\x32..operations_research.sat.LinearExpressionProto\x12\x17\n\x0f\x61\x63tive_literals\x18\x05 \x03(\x05J\x04\x08\x04\x10\x05\"H\n\x16\x43ircuitConstraintProto\x12\r\n\x05tails\x18\x03 \x03(\x05\x12\r\n\x05heads\x18\x04 \x03(\x05\x12\x10\n\x08literals\x18\x05 \x03(\x05\"j\n\x15RoutesConstraintProto\x12\r\n\x05tails\x18\x01 \x03(\x05\x12\r\n\x05heads\x18\x02 \x03(\x05\x12\x10\n\x08literals\x18\x03 \x03(\x05\x12\x0f\n\x07\x64\x65mands\x18\x04 \x03(\x05\x12\x10\n\x08\x63\x61pacity\x18\x05 \x01(\x03\"E\n\x14TableConstraintProto\x12\x0c\n\x04vars\x18\x01 \x03(\x05\x12\x0e\n\x06values\x18\x02 \x03(\x03\x12\x0f\n\x07negated\x18\x03 \x01(\x08\"=\n\x16InverseConstraintProto\x12\x10\n\x08\x66_direct\x18\x01 \x03(\x05\x12\x11\n\tf_inverse\x18\x02 \x03(\x05\"\xa2\x01\n\x18\x41utomatonConstraintProto\x12\x16\n\x0estarting_state\x18\x02 \x01(\x03\x12\x14\n\x0c\x66inal_states\x18\x03 \x03(\x03\x12\x17\n\x0ftransition_tail\x18\x04 \x03(\x03\x12\x17\n\x0ftransition_head\x18\x05 \x03(\x03\x12\x18\n\x10transition_label\x18\x06 \x03(\x03\x12\x0c\n\x04vars\x18\x07 \x03(\x05\"$\n\x14ListOfVariablesProto\x12\x0c\n\x04vars\x18\x01 \x03(\x05\"\xf0\x0c\n\x0f\x43onstraintProto\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x1b\n\x13\x65nforcement_literal\x18\x02 \x03(\x05\x12=\n\x07\x62ool_or\x18\x03 \x01(\x0b\x32*.operations_research.sat.BoolArgumentProtoH\x00\x12>\n\x08\x62ool_and\x18\x04 \x01(\x0b\x32*.operations_research.sat.BoolArgumentProtoH\x00\x12\x41\n\x0b\x61t_most_one\x18\x1a \x01(\x0b\x32*.operations_research.sat.BoolArgumentProtoH\x00\x12\x41\n\x0b\x65xactly_one\x18\x1d \x01(\x0b\x32*.operations_research.sat.BoolArgumentProtoH\x00\x12>\n\x08\x62ool_xor\x18\x05 \x01(\x0b\x32*.operations_research.sat.BoolArgumentProtoH\x00\x12?\n\x07int_div\x18\x07 \x01(\x0b\x32,.operations_research.sat.LinearArgumentProtoH\x00\x12?\n\x07int_mod\x18\x08 \x01(\x0b\x32,.operations_research.sat.LinearArgumentProtoH\x00\x12@\n\x08int_prod\x18\x0b \x01(\x0b\x32,.operations_research.sat.LinearArgumentProtoH\x00\x12?\n\x07lin_max\x18\x1b \x01(\x0b\x32,.operations_research.sat.LinearArgumentProtoH\x00\x12@\n\x06linear\x18\x0c \x01(\x0b\x32..operations_research.sat.LinearConstraintProtoH\x00\x12H\n\x08\x61ll_diff\x18\r \x01(\x0b\x32\x34.operations_research.sat.AllDifferentConstraintProtoH\x00\x12\x42\n\x07\x65lement\x18\x0e \x01(\x0b\x32/.operations_research.sat.ElementConstraintProtoH\x00\x12\x42\n\x07\x63ircuit\x18\x0f \x01(\x0b\x32/.operations_research.sat.CircuitConstraintProtoH\x00\x12@\n\x06routes\x18\x17 \x01(\x0b\x32..operations_research.sat.RoutesConstraintProtoH\x00\x12>\n\x05table\x18\x10 \x01(\x0b\x32-.operations_research.sat.TableConstraintProtoH\x00\x12\x46\n\tautomaton\x18\x11 \x01(\x0b\x32\x31.operations_research.sat.AutomatonConstraintProtoH\x00\x12\x42\n\x07inverse\x18\x12 \x01(\x0b\x32/.operations_research.sat.InverseConstraintProtoH\x00\x12\x46\n\treservoir\x18\x18 \x01(\x0b\x32\x31.operations_research.sat.ReservoirConstraintProtoH\x00\x12\x44\n\x08interval\x18\x13 \x01(\x0b\x32\x30.operations_research.sat.IntervalConstraintProtoH\x00\x12G\n\nno_overlap\x18\x14 \x01(\x0b\x32\x31.operations_research.sat.NoOverlapConstraintProtoH\x00\x12L\n\rno_overlap_2d\x18\x15 \x01(\x0b\x32\x33.operations_research.sat.NoOverlap2DConstraintProtoH\x00\x12H\n\ncumulative\x18\x16 \x01(\x0b\x32\x32.operations_research.sat.CumulativeConstraintProtoH\x00\x12I\n\x10\x64ummy_constraint\x18\x1e \x01(\x0b\x32-.operations_research.sat.ListOfVariablesProtoH\x00\x42\x0c\n\nconstraint\"\xe0\x01\n\x10\x43pObjectiveProto\x12\x0c\n\x04vars\x18\x01 \x03(\x05\x12\x0e\n\x06\x63oeffs\x18\x04 \x03(\x03\x12\x0e\n\x06offset\x18\x02 \x01(\x01\x12\x16\n\x0escaling_factor\x18\x03 \x01(\x01\x12\x0e\n\x06\x64omain\x18\x05 \x03(\x03\x12\x19\n\x11scaling_was_exact\x18\x06 \x01(\x08\x12\x1d\n\x15integer_before_offset\x18\x07 \x01(\x03\x12\x1c\n\x14integer_after_offset\x18\t \x01(\x03\x12\x1e\n\x16integer_scaling_factor\x18\x08 \x01(\x03\"U\n\x13\x46loatObjectiveProto\x12\x0c\n\x04vars\x18\x01 \x03(\x05\x12\x0e\n\x06\x63oeffs\x18\x02 \x03(\x01\x12\x0e\n\x06offset\x18\x03 \x01(\x01\x12\x10\n\x08maximize\x18\x04 \x01(\x08\"\xd7\x05\n\x15\x44\x65\x63isionStrategyProto\x12\x11\n\tvariables\x18\x01 \x03(\x05\x12m\n\x1bvariable_selection_strategy\x18\x02 \x01(\x0e\x32H.operations_research.sat.DecisionStrategyProto.VariableSelectionStrategy\x12i\n\x19\x64omain_reduction_strategy\x18\x03 \x01(\x0e\x32\x46.operations_research.sat.DecisionStrategyProto.DomainReductionStrategy\x12\\\n\x0ftransformations\x18\x04 \x03(\x0b\x32\x43.operations_research.sat.DecisionStrategyProto.AffineTransformation\x1aM\n\x14\x41\x66\x66ineTransformation\x12\r\n\x05index\x18\x01 \x01(\x05\x12\x0e\n\x06offset\x18\x02 \x01(\x03\x12\x16\n\x0epositive_coeff\x18\x03 \x01(\x03\"\x94\x01\n\x19VariableSelectionStrategy\x12\x10\n\x0c\x43HOOSE_FIRST\x10\x00\x12\x15\n\x11\x43HOOSE_LOWEST_MIN\x10\x01\x12\x16\n\x12\x43HOOSE_HIGHEST_MAX\x10\x02\x12\x1a\n\x16\x43HOOSE_MIN_DOMAIN_SIZE\x10\x03\x12\x1a\n\x16\x43HOOSE_MAX_DOMAIN_SIZE\x10\x04\"\x8c\x01\n\x17\x44omainReductionStrategy\x12\x14\n\x10SELECT_MIN_VALUE\x10\x00\x12\x14\n\x10SELECT_MAX_VALUE\x10\x01\x12\x15\n\x11SELECT_LOWER_HALF\x10\x02\x12\x15\n\x11SELECT_UPPER_HALF\x10\x03\x12\x17\n\x13SELECT_MEDIAN_VALUE\x10\x04\"9\n\x19PartialVariableAssignment\x12\x0c\n\x04vars\x18\x01 \x03(\x05\x12\x0e\n\x06values\x18\x02 \x03(\x03\">\n\x16SparsePermutationProto\x12\x0f\n\x07support\x18\x01 \x03(\x05\x12\x13\n\x0b\x63ycle_sizes\x18\x02 \x03(\x05\"G\n\x10\x44\x65nseMatrixProto\x12\x10\n\x08num_rows\x18\x01 \x01(\x05\x12\x10\n\x08num_cols\x18\x02 \x01(\x05\x12\x0f\n\x07\x65ntries\x18\x03 \x03(\x05\"\x94\x01\n\rSymmetryProto\x12\x45\n\x0cpermutations\x18\x01 \x03(\x0b\x32/.operations_research.sat.SparsePermutationProto\x12<\n\torbitopes\x18\x02 \x03(\x0b\x32).operations_research.sat.DenseMatrixProto\"\x8e\x04\n\x0c\x43pModelProto\x12\x0c\n\x04name\x18\x01 \x01(\t\x12@\n\tvariables\x18\x02 \x03(\x0b\x32-.operations_research.sat.IntegerVariableProto\x12=\n\x0b\x63onstraints\x18\x03 \x03(\x0b\x32(.operations_research.sat.ConstraintProto\x12<\n\tobjective\x18\x04 \x01(\x0b\x32).operations_research.sat.CpObjectiveProto\x12N\n\x18\x66loating_point_objective\x18\t \x01(\x0b\x32,.operations_research.sat.FloatObjectiveProto\x12G\n\x0fsearch_strategy\x18\x05 \x03(\x0b\x32..operations_research.sat.DecisionStrategyProto\x12I\n\rsolution_hint\x18\x06 \x01(\x0b\x32\x32.operations_research.sat.PartialVariableAssignment\x12\x13\n\x0b\x61ssumptions\x18\x07 \x03(\x05\x12\x38\n\x08symmetry\x18\x08 \x01(\x0b\x32&.operations_research.sat.SymmetryProto\"\"\n\x10\x43pSolverSolution\x12\x0e\n\x06values\x18\x01 \x03(\x03\"\x95\x06\n\x10\x43pSolverResponse\x12\x37\n\x06status\x18\x01 \x01(\x0e\x32\'.operations_research.sat.CpSolverStatus\x12\x10\n\x08solution\x18\x02 \x03(\x03\x12\x17\n\x0fobjective_value\x18\x03 \x01(\x01\x12\x1c\n\x14\x62\x65st_objective_bound\x18\x04 \x01(\x01\x12G\n\x14\x61\x64\x64itional_solutions\x18\x1b \x03(\x0b\x32).operations_research.sat.CpSolverSolution\x12J\n\x13tightened_variables\x18\x15 \x03(\x0b\x32-.operations_research.sat.IntegerVariableProto\x12\x30\n(sufficient_assumptions_for_infeasibility\x18\x17 \x03(\x05\x12\x44\n\x11integer_objective\x18\x1c \x01(\x0b\x32).operations_research.sat.CpObjectiveProto\x12#\n\x1binner_objective_lower_bound\x18\x1d \x01(\x03\x12\x14\n\x0cnum_integers\x18\x1e \x01(\x03\x12\x14\n\x0cnum_booleans\x18\n \x01(\x03\x12\x15\n\rnum_conflicts\x18\x0b \x01(\x03\x12\x14\n\x0cnum_branches\x18\x0c \x01(\x03\x12\x1f\n\x17num_binary_propagations\x18\r \x01(\x03\x12 \n\x18num_integer_propagations\x18\x0e \x01(\x03\x12\x14\n\x0cnum_restarts\x18\x18 \x01(\x03\x12\x19\n\x11num_lp_iterations\x18\x19 \x01(\x03\x12\x11\n\twall_time\x18\x0f \x01(\x01\x12\x11\n\tuser_time\x18\x10 \x01(\x01\x12\x1a\n\x12\x64\x65terministic_time\x18\x11 \x01(\x01\x12\x14\n\x0cgap_integral\x18\x16 \x01(\x01\x12\x15\n\rsolution_info\x18\x14 \x01(\t\x12\x11\n\tsolve_log\x18\x1a \x01(\t*[\n\x0e\x43pSolverStatus\x12\x0b\n\x07UNKNOWN\x10\x00\x12\x11\n\rMODEL_INVALID\x10\x01\x12\x0c\n\x08\x46\x45\x41SIBLE\x10\x02\x12\x0e\n\nINFEASIBLE\x10\x03\x12\x0b\n\x07OPTIMAL\x10\x04\x42@\n\x16\x63om.google.ortools.satB\x0f\x43pModelProtobufP\x01\xaa\x02\x12Google.OrTools.Satb\x06proto3') 17 18_globals = globals() 19_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) 20_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'ortools.sat.cp_model_pb2', _globals) 21if _descriptor._USE_C_DESCRIPTORS == False: 22 DESCRIPTOR._options = None 23 DESCRIPTOR._serialized_options = b'\n\026com.google.ortools.satB\017CpModelProtobufP\001\252\002\022Google.OrTools.Sat' 24 _globals['_CPSOLVERSTATUS']._serialized_start=6274 25 _globals['_CPSOLVERSTATUS']._serialized_end=6365 26 _globals['_INTEGERVARIABLEPROTO']._serialized_start=55 27 _globals['_INTEGERVARIABLEPROTO']._serialized_end=107 28 _globals['_BOOLARGUMENTPROTO']._serialized_start=109 29 _globals['_BOOLARGUMENTPROTO']._serialized_end=146 30 _globals['_LINEAREXPRESSIONPROTO']._serialized_start=148 31 _globals['_LINEAREXPRESSIONPROTO']._serialized_end=217 32 _globals['_LINEARARGUMENTPROTO']._serialized_start=220 33 _globals['_LINEARARGUMENTPROTO']._serialized_end=368 34 _globals['_ALLDIFFERENTCONSTRAINTPROTO']._serialized_start=370 35 _globals['_ALLDIFFERENTCONSTRAINTPROTO']._serialized_end=462 36 _globals['_LINEARCONSTRAINTPROTO']._serialized_start=464 37 _globals['_LINEARCONSTRAINTPROTO']._serialized_end=533 38 _globals['_ELEMENTCONSTRAINTPROTO']._serialized_start=535 39 _globals['_ELEMENTCONSTRAINTPROTO']._serialized_end=604 40 _globals['_INTERVALCONSTRAINTPROTO']._serialized_start=607 41 _globals['_INTERVALCONSTRAINTPROTO']._serialized_end=818 42 _globals['_NOOVERLAPCONSTRAINTPROTO']._serialized_start=820 43 _globals['_NOOVERLAPCONSTRAINTPROTO']._serialized_end=865 44 _globals['_NOOVERLAP2DCONSTRAINTPROTO']._serialized_start=867 45 _globals['_NOOVERLAP2DCONSTRAINTPROTO']._serialized_end=937 46 _globals['_CUMULATIVECONSTRAINTPROTO']._serialized_start=940 47 _globals['_CUMULATIVECONSTRAINTPROTO']._serialized_end=1117 48 _globals['_RESERVOIRCONSTRAINTPROTO']._serialized_start=1120 49 _globals['_RESERVOIRCONSTRAINTPROTO']._serialized_end=1354 50 _globals['_CIRCUITCONSTRAINTPROTO']._serialized_start=1356 51 _globals['_CIRCUITCONSTRAINTPROTO']._serialized_end=1428 52 _globals['_ROUTESCONSTRAINTPROTO']._serialized_start=1430 53 _globals['_ROUTESCONSTRAINTPROTO']._serialized_end=1536 54 _globals['_TABLECONSTRAINTPROTO']._serialized_start=1538 55 _globals['_TABLECONSTRAINTPROTO']._serialized_end=1607 56 _globals['_INVERSECONSTRAINTPROTO']._serialized_start=1609 57 _globals['_INVERSECONSTRAINTPROTO']._serialized_end=1670 58 _globals['_AUTOMATONCONSTRAINTPROTO']._serialized_start=1673 59 _globals['_AUTOMATONCONSTRAINTPROTO']._serialized_end=1835 60 _globals['_LISTOFVARIABLESPROTO']._serialized_start=1837 61 _globals['_LISTOFVARIABLESPROTO']._serialized_end=1873 62 _globals['_CONSTRAINTPROTO']._serialized_start=1876 63 _globals['_CONSTRAINTPROTO']._serialized_end=3524 64 _globals['_CPOBJECTIVEPROTO']._serialized_start=3527 65 _globals['_CPOBJECTIVEPROTO']._serialized_end=3751 66 _globals['_FLOATOBJECTIVEPROTO']._serialized_start=3753 67 _globals['_FLOATOBJECTIVEPROTO']._serialized_end=3838 68 _globals['_DECISIONSTRATEGYPROTO']._serialized_start=3841 69 _globals['_DECISIONSTRATEGYPROTO']._serialized_end=4568 70 _globals['_DECISIONSTRATEGYPROTO_AFFINETRANSFORMATION']._serialized_start=4197 71 _globals['_DECISIONSTRATEGYPROTO_AFFINETRANSFORMATION']._serialized_end=4274 72 _globals['_DECISIONSTRATEGYPROTO_VARIABLESELECTIONSTRATEGY']._serialized_start=4277 73 _globals['_DECISIONSTRATEGYPROTO_VARIABLESELECTIONSTRATEGY']._serialized_end=4425 74 _globals['_DECISIONSTRATEGYPROTO_DOMAINREDUCTIONSTRATEGY']._serialized_start=4428 75 _globals['_DECISIONSTRATEGYPROTO_DOMAINREDUCTIONSTRATEGY']._serialized_end=4568 76 _globals['_PARTIALVARIABLEASSIGNMENT']._serialized_start=4570 77 _globals['_PARTIALVARIABLEASSIGNMENT']._serialized_end=4627 78 _globals['_SPARSEPERMUTATIONPROTO']._serialized_start=4629 79 _globals['_SPARSEPERMUTATIONPROTO']._serialized_end=4691 80 _globals['_DENSEMATRIXPROTO']._serialized_start=4693 81 _globals['_DENSEMATRIXPROTO']._serialized_end=4764 82 _globals['_SYMMETRYPROTO']._serialized_start=4767 83 _globals['_SYMMETRYPROTO']._serialized_end=4915 84 _globals['_CPMODELPROTO']._serialized_start=4918 85 _globals['_CPMODELPROTO']._serialized_end=5444 86 _globals['_CPSOLVERSOLUTION']._serialized_start=5446 87 _globals['_CPSOLVERSOLUTION']._serialized_end=5480 88 _globals['_CPSOLVERRESPONSE']._serialized_start=5483 89 _globals['_CPSOLVERRESPONSE']._serialized_end=6272 90# @@protoc_insertion_point(module_scope)
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
Inherited Members
- AffineTransformation
- AffineTransformation
- DESCRIPTOR
- MergeFrom
- Clear
- SetInParent
- IsInitialized
- MergeFromString
- SerializeToString
- SerializePartialToString
- ListFields
- HasField
- ClearField
- WhichOneof
- UnknownFields
- DiscardUnknownFields
- ByteSize
- FromString
- RegisterExtension
- INDEX_FIELD_NUMBER
- index
- OFFSET_FIELD_NUMBER
- offset
- POSITIVE_COEFF_FIELD_NUMBER
- positive_coeff
- FindInitializationErrors
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
488 def init(self, **kwargs): 489 self._cached_byte_size = 0 490 self._cached_byte_size_dirty = len(kwargs) > 0 491 self._fields = {} 492 # Contains a mapping from oneof field descriptors to the descriptor 493 # of the currently set field in that oneof field. 494 self._oneofs = {} 495 496 # _unknown_fields is () when empty for efficiency, and will be turned into 497 # a list if fields are added. 498 self._unknown_fields = () 499 # _unknown_field_set is None when empty for efficiency, and will be 500 # turned into UnknownFieldSet struct if fields are added. 501 self._unknown_field_set = None # pylint: disable=protected-access 502 self._is_present_in_parent = False 503 self._listener = message_listener_mod.NullMessageListener() 504 self._listener_for_children = _Listener(self) 505 for field_name, field_value in kwargs.items(): 506 field = _GetFieldByName(message_descriptor, field_name) 507 if field is None: 508 raise TypeError('%s() got an unexpected keyword argument "%s"' % 509 (message_descriptor.name, field_name)) 510 if field_value is None: 511 # field=None is the same as no field at all. 512 continue 513 if field.label == _FieldDescriptor.LABEL_REPEATED: 514 copy = field._default_constructor(self) 515 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 516 if _IsMapField(field): 517 if _IsMessageMapField(field): 518 for key in field_value: 519 copy[key].MergeFrom(field_value[key]) 520 else: 521 copy.update(field_value) 522 else: 523 for val in field_value: 524 if isinstance(val, dict): 525 copy.add(**val) 526 else: 527 copy.add().MergeFrom(val) 528 else: # Scalar 529 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 530 field_value = [_GetIntegerEnumValue(field.enum_type, val) 531 for val in field_value] 532 copy.extend(field_value) 533 self._fields[field] = copy 534 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 535 copy = field._default_constructor(self) 536 new_val = field_value 537 if isinstance(field_value, dict): 538 new_val = field.message_type._concrete_class(**field_value) 539 try: 540 copy.MergeFrom(new_val) 541 except TypeError: 542 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 543 self._fields[field] = copy 544 else: 545 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 546 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 547 try: 548 setattr(self, field_name, field_value) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1288 def MergeFrom(self, msg): 1289 if not isinstance(msg, cls): 1290 raise TypeError( 1291 'Parameter to MergeFrom() must be instance of same class: ' 1292 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1293 _FullyQualifiedClassName(msg.__class__))) 1294 1295 assert msg is not self 1296 self._Modified() 1297 1298 fields = self._fields 1299 1300 for field, value in msg._fields.items(): 1301 if field.label == LABEL_REPEATED: 1302 field_value = fields.get(field) 1303 if field_value is None: 1304 # Construct a new object to represent this field. 1305 field_value = field._default_constructor(self) 1306 fields[field] = field_value 1307 field_value.MergeFrom(value) 1308 elif field.cpp_type == CPPTYPE_MESSAGE: 1309 if value._is_present_in_parent: 1310 field_value = fields.get(field) 1311 if field_value is None: 1312 # Construct a new object to represent this field. 1313 field_value = field._default_constructor(self) 1314 fields[field] = field_value 1315 field_value.MergeFrom(value) 1316 else: 1317 self._fields[field] = value 1318 if field.containing_oneof: 1319 self._UpdateOneofState(field) 1320 1321 if msg._unknown_fields: 1322 if not self._unknown_fields: 1323 self._unknown_fields = [] 1324 self._unknown_fields.extend(msg._unknown_fields) 1325 # pylint: disable=protected-access 1326 if self._unknown_field_set is None: 1327 self._unknown_field_set = containers.UnknownFieldSet() 1328 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1351def _Clear(self): 1352 # Clear fields. 1353 self._fields = {} 1354 self._unknown_fields = () 1355 # pylint: disable=protected-access 1356 if self._unknown_field_set is not None: 1357 self._unknown_field_set._clear() 1358 self._unknown_field_set = None 1359 1360 self._oneofs = {} 1361 self._Modified()
Clears all data that was set in the message.
1423 def Modified(self): 1424 """Sets the _cached_byte_size_dirty bit to true, 1425 and propagates this to our listener iff this was a state change. 1426 """ 1427 1428 # Note: Some callers check _cached_byte_size_dirty before calling 1429 # _Modified() as an extra optimization. So, if this method is ever 1430 # changed such that it does stuff even when _cached_byte_size_dirty is 1431 # already true, the callers need to be updated. 1432 if not self._cached_byte_size_dirty: 1433 self._cached_byte_size_dirty = True 1434 self._listener_for_children.dirty = True 1435 self._is_present_in_parent = True 1436 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1188 def IsInitialized(self, errors=None): 1189 """Checks if all required fields of a message are set. 1190 1191 Args: 1192 errors: A list which, if provided, will be populated with the field 1193 paths of all missing required fields. 1194 1195 Returns: 1196 True iff the specified message has all required fields set. 1197 """ 1198 1199 # Performance is critical so we avoid HasField() and ListFields(). 1200 1201 for field in required_fields: 1202 if (field not in self._fields or 1203 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1204 not self._fields[field]._is_present_in_parent)): 1205 if errors is not None: 1206 errors.extend(self.FindInitializationErrors()) 1207 return False 1208 1209 for field, value in list(self._fields.items()): # dict can change size! 1210 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1211 if field.label == _FieldDescriptor.LABEL_REPEATED: 1212 if (field.message_type.has_options and 1213 field.message_type.GetOptions().map_entry): 1214 continue 1215 for element in value: 1216 if not element.IsInitialized(): 1217 if errors is not None: 1218 errors.extend(self.FindInitializationErrors()) 1219 return False 1220 elif value._is_present_in_parent and not value.IsInitialized(): 1221 if errors is not None: 1222 errors.extend(self.FindInitializationErrors()) 1223 return False 1224 1225 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1103 def MergeFromString(self, serialized): 1104 serialized = memoryview(serialized) 1105 length = len(serialized) 1106 try: 1107 if self._InternalParse(serialized, 0, length) != length: 1108 # The only reason _InternalParse would return early is if it 1109 # encountered an end-group tag. 1110 raise message_mod.DecodeError('Unexpected end-group tag.') 1111 except (IndexError, TypeError): 1112 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1113 raise message_mod.DecodeError('Truncated message.') 1114 except struct.error as e: 1115 raise message_mod.DecodeError(e) 1116 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized. For non-group messages, this will always belen(serialized), but for messages which are actually groups, this will generally be less thanlen(serialized), since we must stop when we reach anEND_GROUPtag. Note that if we do stop because of anEND_GROUPtag, the number of bytes returned does not include the bytes for theEND_GROUPtag information.
Raises:
- DecodeError: if the input cannot be parsed.
1059 def SerializeToString(self, **kwargs): 1060 # Check if the message has all of its required fields set. 1061 if not self.IsInitialized(): 1062 raise message_mod.EncodeError( 1063 'Message %s is missing required fields: %s' % ( 1064 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1065 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()).
1072 def SerializePartialToString(self, **kwargs): 1073 out = BytesIO() 1074 self._InternalSerialize(out.write, **kwargs) 1075 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
806 def ListFields(self): 807 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 808 all_fields.sort(key = lambda item: item[0].number) 809 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
830 def HasField(self, field_name): 831 try: 832 field = hassable_fields[field_name] 833 except KeyError as exc: 834 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 835 'nor has presence is not available for this field.' % ( 836 message_descriptor.full_name, field_name)) from exc 837 838 if isinstance(field, descriptor_mod.OneofDescriptor): 839 try: 840 return HasField(self, self._oneofs[field].name) 841 except KeyError: 842 return False 843 else: 844 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 845 value = self._fields.get(field) 846 return value is not None and value._is_present_in_parent 847 else: 848 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_nameis not a member of this message.
855 def ClearField(self, field_name): 856 try: 857 field = message_descriptor.fields_by_name[field_name] 858 except KeyError: 859 try: 860 field = message_descriptor.oneofs_by_name[field_name] 861 if field in self._oneofs: 862 field = self._oneofs[field] 863 else: 864 return 865 except KeyError: 866 raise ValueError('Protocol message %s has no "%s" field.' % 867 (message_descriptor.name, field_name)) 868 869 if field in self._fields: 870 # To match the C++ implementation, we need to invalidate iterators 871 # for map fields when ClearField() happens. 872 if hasattr(self._fields[field], 'InvalidateIterators'): 873 self._fields[field].InvalidateIterators() 874 875 # Note: If the field is a sub-message, its listener will still point 876 # at us. That's fine, because the worst than can happen is that it 877 # will call _Modified() and invalidate our byte size. Big deal. 878 del self._fields[field] 879 880 if self._oneofs.get(field.containing_oneof, None) is field: 881 del self._oneofs[field.containing_oneof] 882 883 # Always call _Modified() -- even if nothing was changed, this is 884 # a mutating method, and thus calling it should cause the field to become 885 # present in the parent message. 886 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_nameis not a member of this message.
1334 def WhichOneof(self, oneof_name): 1335 """Returns the name of the currently set field inside a oneof, or None.""" 1336 try: 1337 field = message_descriptor.oneofs_by_name[oneof_name] 1338 except KeyError: 1339 raise ValueError( 1340 'Protocol message has no oneof "%s" field.' % oneof_name) 1341 1342 nested_field = self._oneofs.get(field, None) 1343 if nested_field is not None and self.HasField(nested_field.name): 1344 return nested_field.name 1345 else: 1346 return None
Returns the name of the currently set field inside a oneof, or None.
1364def _UnknownFields(self): 1365 if self._unknown_field_set is None: # pylint: disable=protected-access 1366 # pylint: disable=protected-access 1367 self._unknown_field_set = containers.UnknownFieldSet() 1368 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1371def _DiscardUnknownFields(self): 1372 self._unknown_fields = [] 1373 self._unknown_field_set = None # pylint: disable=protected-access 1374 for field, value in self.ListFields(): 1375 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1376 if _IsMapField(field): 1377 if _IsMessageMapField(field): 1378 for key in value: 1379 value[key].DiscardUnknownFields() 1380 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1381 for sub_message in value: 1382 sub_message.DiscardUnknownFields() 1383 else: 1384 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet.
This operation is recursive for nested message.
1032 def ByteSize(self): 1033 if not self._cached_byte_size_dirty: 1034 return self._cached_byte_size 1035 1036 size = 0 1037 descriptor = self.DESCRIPTOR 1038 if descriptor.GetOptions().map_entry: 1039 # Fields of map entry should always be serialized. 1040 size = descriptor.fields_by_name['key']._sizer(self.key) 1041 size += descriptor.fields_by_name['value']._sizer(self.value) 1042 else: 1043 for field_descriptor, field_value in self.ListFields(): 1044 size += field_descriptor._sizer(field_value) 1045 for tag_bytes, value_bytes in self._unknown_fields: 1046 size += len(tag_bytes) + len(value_bytes) 1047 1048 self._cached_byte_size = size 1049 self._cached_byte_size_dirty = False 1050 self._listener_for_children.dirty = False 1051 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
776 def RegisterExtension(field_descriptor): 777 field_descriptor.containing_type = cls.DESCRIPTOR 778 # TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available. 779 # pylint: disable=protected-access 780 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 781 _AttachFieldHelpers(cls, field_descriptor)
1229 def FindInitializationErrors(self): 1230 """Finds required fields which are not initialized. 1231 1232 Returns: 1233 A list of strings. Each string is a path to an uninitialized field from 1234 the top-level message, e.g. "foo.bar[5].baz". 1235 """ 1236 1237 errors = [] # simplify things 1238 1239 for field in required_fields: 1240 if not self.HasField(field.name): 1241 errors.append(field.name) 1242 1243 for field, value in self.ListFields(): 1244 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1245 if field.is_extension: 1246 name = '(%s)' % field.full_name 1247 else: 1248 name = field.name 1249 1250 if _IsMapField(field): 1251 if _IsMessageMapField(field): 1252 for key in value: 1253 element = value[key] 1254 prefix = '%s[%s].' % (name, key) 1255 sub_errors = element.FindInitializationErrors() 1256 errors += [prefix + error for error in sub_errors] 1257 else: 1258 # ScalarMaps can't have any initialization errors. 1259 pass 1260 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1261 for i in range(len(value)): 1262 element = value[i] 1263 prefix = '%s[%d].' % (name, i) 1264 sub_errors = element.FindInitializationErrors() 1265 errors += [prefix + error for error in sub_errors] 1266 else: 1267 prefix = name + '.' 1268 sub_errors = value.FindInitializationErrors() 1269 errors += [prefix + error for error in sub_errors] 1270 1271 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension